package com.shujia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MysqlDemo1 {
    public static void main(String[] args) {
        Connection conn = null;
        try {
            //1.注册驱动(告诉1Java程序，即将链接的是哪个品牌的数据库)
            Class.forName("com.mysql.jdbc.Driver"); //通过反射来创建连接
            //2.获取连接(表示JVM的进程和数据库进程之间的通道打开了，这属于进程之间的通信，用完要关闭)
            conn = DriverManager.getConnection("jdbc:mysql://192.168.11.100:3306/DemoTest1?characterEncoding=utf-8", "root", "123456");

            System.out.println("成功与mysql连接成功"+conn);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        Statement statement= null;
        String sql = "update salgrade set hisal = 10000 where grade=1";
        try {
             statement = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int count = 0;
        try {
            count = statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(count);
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
